package BNS.wheat;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import BNS.graphs.*;

/**  shows acquired and endowment values on master graph (for Supervisor) */
public class WealthPanel extends GraphPanel {
	
	private NodeState[] nodeStates;
	private static final Color money= new Color(0,255,128),
		wealth= new Color(230,200,0);
	
	/** A Graph and an array of NodeState s are accepted as defining the display.
		The Graph is presumed not to change, but the values in the NodeState array are
		presumed to change constantly as the game progresses.
		ns must be have as many entries as gr has nodes, 
		and they must be arranged in order of node index. */
	public WealthPanel(Graph gr, NodeState[] ns) { 
		super();
		super.init(gr);
		System.err.println("WealthPanel init"); 
		nodeStates= ns;
//		System.err.println("graph has "+graph.numNodes()+" nodes & "+graph.numEdges()+" edges");
		waitMessage= "panel created";
		waitMessage= null;
		}

	public synchronized void paint(Graphics g) {
		if (!ready(g)) return;
		drawEdges();
		drawNodes();
		commitImage(g);
		}
	
    protected void paintNode(Graphics g, Node n, ColorMap render) {
		Point pt= n.position();
		int x= (int)pt.x;
		int y= (int)pt.y;
		int r= 2*nodeRadius;
		g.setColor(Color.white);
		g.fillOval(x-r/2, y-r/2,  r,r);
		
		r= rad(nodeStates[n.getIndex()].endowment+nodeStates[n.getIndex()].acquired);
//		System.err.format("WealthPanel paintNode bigRadius %f %f %d%n",
//					  nodeStates[n.getIndex()].endowment,nodeStates[n.getIndex()].acquired,r); 
		g.setColor(wealth);
		g.fillOval(x-r/2, y-r/2,  r,r);
		
		r= rad(nodeStates[n.getIndex()].endowment);
		g.setColor(money);
		g.fillOval(x-r/2, y-r/2,  r,r);
		
		r= 2*nodeRadius;
		g.setColor(Color.black);
		g.drawOval(x-r/2, y-r/2,  r+1,r+1);		// drawOval needs 1 more pixel than fillOval
		
//		r= (int)(Math.log(nodeStates[n.getIndex()].sellingPrice)/Math.log(6.0)*nodeRadius);
		r= (int)(Math.atan(nodeStates[n.getIndex()].sellingPrice)*4/Math.PI *nodeRadius);
//		System.err.format("WealthPanel paintNode priceline %f %d%n",
//						  nodeStates[n.getIndex()].sellingPrice,r);
		g.drawLine(x-nodeRadius,y+nodeRadius-r,x+nodeRadius,y+nodeRadius-r);
		
		g.drawString(n.lbl, x-fontmetrics.stringWidth(n.lbl)/2, y+halfChar);
		}
	
	private int rad(float area) {
		double r= Math.sqrt(area/WheatMktGame.initialEndowment)*nodeRadius*2;
		return (int)r;
		}
	
	}
